//
//  AppDelegate.m
//  引导页
//
//  Created by mafeng on 16/7/20.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"

#define WIDTH self.window.frame.size.width
#define HEIGHT self.window.frame.size.height
BOOL isOut = NO;
@interface AppDelegate ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *page;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _window.rootViewController = [[ViewController alloc] init];
    [_window makeKeyAndVisible];
    
    // 使用系统偏好的设置提供好的"单例对象"偏好设置类
    NSUserDefaults *user = [[NSUserDefaults alloc] init];
    // 只有第一次进来是引导页
    if (![user objectForKey:@"hae"]) {
        // 放图片
        [self makeLaunchView];
        [self addPage];
        [user setObject:@"yes" forKey:@"hae"];
       
    }
    return YES;
}
- (void)makeLaunchView {
    // 数组内存放的是我要现实的引导页的图片
    NSArray *array = [NSArray arrayWithObjects:@"guidePage_01", @"guidePage_02", @"guidePage_03", @"guidePage_04", nil];
    // 通过scrollView将这些图片添加到上面,从而达到滚动这些图片
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    scrollView.contentSize = CGSizeMake(WIDTH *array.count, HEIGHT);
    // 分页效果
    scrollView.pagingEnabled = YES;
    // 监听滑动
    scrollView.delegate = self;
    [_window addSubview:scrollView];
    for (int i = 0; i < array.count; i ++) {
        UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(i * WIDTH, 0, WIDTH, HEIGHT)];
        img.image = [UIImage imageNamed:array[i]];
        [scrollView addSubview:img];
    }
//
}
- (void)addPage {
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, HEIGHT - 80, 100, 40)];
    // 当前页
    self.page.currentPage = 0;
    self.page.numberOfPages = 4;
    [_window addSubview:self.page];
}
// scrollView的协议方法
// 第一类: 滚动监听 控制点: 把偏移量转化为contentOffset为点currentPage
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    // 这里是在滚动的时候判断我滚动到那张图片了, 如果滚动到最后一张图片, 那么 往下滑动就应该进入主页面, 我这里利用偏移量来判断的, 一共有四张图片, 所以当图片全部划动完之后, 又向后多滑动一段距离的时候就做下一个动作
    if (scrollView.contentOffset.x > 3 * WIDTH + 30) {
        isOut = YES;
        // 这是我声明的一个全局变量BOOL类型, 初始化为NO, 当达到需要的条件改为yes
    }
}
// 已经完成减速的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / scrollView.frame.size.width;
    //让点控制图: 把currentPage转化为contentOffset
    self.page.currentPage = index;
    // 如果isOut为真, 进入主界面
    if (isOut) {
    [UIView animateWithDuration:1.5 animations:^{
        // 让scrollView渐变消失
        scrollView.alpha = 0;
    } completion:^(BOOL finished) {
        // 将scrollview移除
        [scrollView removeFromSuperview];
        self.page.hidden = YES;
    }];
    }
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
